package cn.uppower.wordaliy.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.widget.BaseActivity;

public class SplashActivity extends BaseActivity {



    private static final int START_COUNT=100;
    private TextView tv_splash_time;
    private int nowMill=4;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==100){
                if (nowMill!=0){
                    nowMill--;
                    tv_splash_time.setText("跳过 "+nowMill+"s");
                    handler.sendEmptyMessageDelayed(START_COUNT,1000);
                }else {
                    startNewActivity();
                    finish();
                }

            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.activity_splash;

    }

    @Override
    public void init() {

        tv_splash_time = findViewById(R.id.tv_splash_time);
        tv_splash_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startNewActivity();
                finish();
            }
        });

        handler.sendEmptyMessage(START_COUNT);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(START_COUNT);
    }


    private void startNewActivity(){
        if (WordaliyApplication.userInfo.getUserPhone()==null||WordaliyApplication.userInfo.getUserPhone().equals("")){
//            说明没有登录过，进入登录界面
            startActivity(new Intent(SplashActivity.this,LoginActivity.class));
        }else {
//            直接进入主界面
            startActivity(new Intent(SplashActivity.this,MainActivity.class));

        }
    }
}
